package com.ksd.modules.special.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ksd.common.core.exception.KsdBusinessException;
import com.ksd.common.enums.DeletedEnums;
import com.ksd.common.web.threadlocal.SystemIdThrealLocal;
import com.ksd.commons.BaseServiceImpl;
import com.ksd.modules.course.mapper.AdminCourseMapper;
import com.ksd.modules.download.entity.AdminDownloads;
import com.ksd.modules.download.mapper.AdminDownloadsMapper;
import com.ksd.modules.members.entity.AdminUser;
import com.ksd.modules.members.mapper.AdminUsersMapper;
import com.ksd.modules.note.entity.AdminNote;
import com.ksd.modules.note.mapper.AdminNoteMapper;
import com.ksd.modules.special.entity.AdminSpecialBindDetail;
import com.ksd.modules.special.mapper.AdminSpecialBindDetailMapper;
import com.ksd.modules.special.po.AdminSpecialBindDetailSavePO;
import com.ksd.modules.special.service.IAdminSpecialBindDetailService;
import com.ksd.modules.special.vo.AdminSpecialBindDetailVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdminSpecialBindDetailServiceImpl extends BaseServiceImpl<AdminSpecialBindDetailMapper, AdminSpecialBindDetail> implements IAdminSpecialBindDetailService {


    @Autowired
    private AdminCourseMapper adminCourseMapper;
    @Autowired
    private AdminDownloadsMapper adminDownloadsMapper;
    @Autowired
    private AdminNoteMapper adminNoteMapper;
    @Autowired
    private AdminUsersMapper adminUsersMapper;


    @Override
    public List<AdminSpecialBindDetailVo> findAdminSpecialBindDetails(Long specialId, Long bindId, Integer type) {
        LambdaQueryWrapper<AdminSpecialBindDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSpecialBindDetail::getSpecialId, specialId);
        lambdaQueryWrapper.eq(AdminSpecialBindDetail::getBindId, bindId);
        lambdaQueryWrapper.eq(AdminSpecialBindDetail::getIsDeleted, DeletedEnums.UN_DELETED.getCode());
        lambdaQueryWrapper.eq(AdminSpecialBindDetail::getSystemId, SystemIdThrealLocal.get().getSystemId());
        lambdaQueryWrapper.orderByAsc(AdminSpecialBindDetail::getSorted);
        List<AdminSpecialBindDetailVo> adminSpecialBindDetailVos = tranferListBo(this.list(lambdaQueryWrapper), AdminSpecialBindDetailVo.class);
        if (CollectionUtils.isNotEmpty(adminSpecialBindDetailVos)) {
            // 小册
            if (type.equals(1)) {
                List<Long> noteIds = adminSpecialBindDetailVos.stream().map(c -> c.getNoteId()).collect(Collectors.toList());
                LambdaQueryWrapper<AdminNote> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper1.in(AdminNote::getId, noteIds);
                List<AdminNote> adminNoteList = adminNoteMapper.selectList(lambdaQueryWrapper1);
                List<AdminSpecialBindDetailVo> adminSpecialBindDetailVosList = new ArrayList<>();
                for (AdminSpecialBindDetailVo adminSpecialBindDetailVo : adminSpecialBindDetailVos) {
                    for (AdminNote adminNote : adminNoteList) {
                        if(adminNote.getId().equals(adminSpecialBindDetailVo.getNoteId())) {
                            AdminSpecialBindDetailVo nadminSpecialBindDetailVo = tranferBo(adminNote, AdminSpecialBindDetailVo.class);
                            nadminSpecialBindDetailVo.setId(adminSpecialBindDetailVo.getId());
                            nadminSpecialBindDetailVo.setSpecialId(adminSpecialBindDetailVo.getSpecialId());
                            nadminSpecialBindDetailVo.setBindId(adminSpecialBindDetailVo.getBindId());
                            nadminSpecialBindDetailVo.setSorted(adminSpecialBindDetailVo.getSorted());
                            nadminSpecialBindDetailVo.setStatus(adminSpecialBindDetailVo.getStatus());
                            nadminSpecialBindDetailVo.setNoteId(adminNote.getId());
                            adminSpecialBindDetailVosList.add(nadminSpecialBindDetailVo);
                        }
                    }
                }
                return adminSpecialBindDetailVosList;
            }

            // 资源
            if (type.equals(2)) {
                List<Long> downloadIds = adminSpecialBindDetailVos.stream().map(c -> c.getDownloadId()).collect(Collectors.toList());
                LambdaQueryWrapper<AdminDownloads> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper1.in(AdminDownloads::getId, downloadIds);
                List<AdminDownloads> adminDownloadsList = adminDownloadsMapper.selectList(lambdaQueryWrapper1);
                List<AdminSpecialBindDetailVo> adminSpecialBindDetailVosList = new ArrayList<>();
                for (AdminSpecialBindDetailVo adminSpecialBindDetailVo : adminSpecialBindDetailVos) {
                    for (AdminDownloads adminDownloads : adminDownloadsList) {
                        if(adminDownloads.getId().equals(adminSpecialBindDetailVo.getDownloadId())) {
                            AdminSpecialBindDetailVo nadminSpecialBindDetailVo = tranferBo(adminDownloads, AdminSpecialBindDetailVo.class);
                            nadminSpecialBindDetailVo.setId(adminSpecialBindDetailVo.getId());
                            nadminSpecialBindDetailVo.setSpecialId(adminSpecialBindDetailVo.getSpecialId());
                            nadminSpecialBindDetailVo.setBindId(adminSpecialBindDetailVo.getBindId());
                            nadminSpecialBindDetailVo.setSorted(adminSpecialBindDetailVo.getSorted());
                            nadminSpecialBindDetailVo.setStatus(adminSpecialBindDetailVo.getStatus());
                            nadminSpecialBindDetailVo.setDownloadId(adminDownloads.getId());
                            adminSpecialBindDetailVosList.add(nadminSpecialBindDetailVo);
                        }
                    }
                }
                return adminSpecialBindDetailVosList;
            }

            // 老师
            if (type.equals(3)) {
                List<Long> teacherIds = adminSpecialBindDetailVos.stream().map(c -> c.getTeacherId()).collect(Collectors.toList());
                LambdaQueryWrapper<AdminUser> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper1.in(AdminUser::getId, teacherIds);
                List<AdminUser> adminUserList = adminUsersMapper.selectList(lambdaQueryWrapper1);
                List<AdminSpecialBindDetailVo> adminSpecialBindDetailVosList = new ArrayList<>();
                for (AdminSpecialBindDetailVo adminSpecialBindDetailVo : adminSpecialBindDetailVos) {
                    for (AdminUser adminUser : adminUserList) {
                        if(adminUser.getId().equals(adminSpecialBindDetailVo.getTeacherId())) {
                            AdminSpecialBindDetailVo cadminSpecialBindDetailVo = tranferBo(adminUser, AdminSpecialBindDetailVo.class);
                            cadminSpecialBindDetailVo.setId(adminSpecialBindDetailVo.getId());
                            cadminSpecialBindDetailVo.setTitle(adminUser.getNickName());
                            cadminSpecialBindDetailVo.setImg(adminUser.getAvatar());
                            cadminSpecialBindDetailVo.setSpecialId(adminSpecialBindDetailVo.getSpecialId());
                            cadminSpecialBindDetailVo.setBindId(adminSpecialBindDetailVo.getBindId());
                            cadminSpecialBindDetailVo.setSorted(adminSpecialBindDetailVo.getSorted());
                            cadminSpecialBindDetailVo.setStatus(adminSpecialBindDetailVo.getStatus());
                            cadminSpecialBindDetailVo.setTeacherId(adminUser.getId());
                            adminSpecialBindDetailVosList.add(cadminSpecialBindDetailVo);
                        }
                    }
                }
                return adminSpecialBindDetailVosList;
            }
        }

        return new ArrayList<>();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUpdateAdminSpecialBindDetail(AdminSpecialBindDetailSavePO adminSpecialBindDetailSavePO) {
        AdminSpecialBindDetail adminSpecialBindDetail = tranferBo(adminSpecialBindDetailSavePO, AdminSpecialBindDetail.class);
        adminSpecialBindDetail.setSystemId(SystemIdThrealLocal.get().getSystemId());
        adminSpecialBindDetail.setSystemName(SystemIdThrealLocal.get().getSystemName());
        // 课程
//        if (adminSpecialBindDetailSavePO.getType().equals(1)) {
//            LambdaQueryWrapper<AdminSpecialBindDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//            lambdaQueryWrapper.eq(AdminSpecialBindDetail::getSpecialId, adminSpecialBindDetailSavePO.getSpecialId());
//            lambdaQueryWrapper.eq(AdminSpecialBindDetail::getBindId, adminSpecialBindDetailSavePO.getBindId());
//            lambdaQueryWrapper.eq(AdminSpecialBindDetail::getCourseId, adminSpecialBindDetailSavePO.getCourseId());
//            long count = this.count(lambdaQueryWrapper);
//            if (count > 0) {
//                throw new ApiException(501,"已经绑定了此课程!");
//            }
//            return this.saveOrUpdate(adminSpecialBindDetail);
//        }

        // 小册
        if (adminSpecialBindDetailSavePO.getType().equals(1)) {
            LambdaQueryWrapper<AdminSpecialBindDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AdminSpecialBindDetail::getSpecialId, adminSpecialBindDetailSavePO.getSpecialId());
            lambdaQueryWrapper.eq(AdminSpecialBindDetail::getBindId, adminSpecialBindDetailSavePO.getBindId());
            lambdaQueryWrapper.eq(AdminSpecialBindDetail::getNoteId, adminSpecialBindDetailSavePO.getNoteId());
            long count = this.count(lambdaQueryWrapper);
            if (count > 0) {
                throw new KsdBusinessException("已经绑定了此小册!");
            }
            return this.saveOrUpdate(adminSpecialBindDetail);
        }

        // 资源
        if (adminSpecialBindDetailSavePO.getType().equals(2)) {
            LambdaQueryWrapper<AdminSpecialBindDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AdminSpecialBindDetail::getSpecialId, adminSpecialBindDetailSavePO.getSpecialId());
            lambdaQueryWrapper.eq(AdminSpecialBindDetail::getBindId, adminSpecialBindDetailSavePO.getBindId());
            lambdaQueryWrapper.eq(AdminSpecialBindDetail::getDownloadId, adminSpecialBindDetailSavePO.getDownloadId());
            long count = this.count(lambdaQueryWrapper);
            if (count > 0) {
                throw new KsdBusinessException("已经绑定了此资源!");
            }
            return this.saveOrUpdate(adminSpecialBindDetail);
        }

        //老师
        if (adminSpecialBindDetailSavePO.getType().equals(3)) {
            LambdaQueryWrapper<AdminSpecialBindDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AdminSpecialBindDetail::getSpecialId, adminSpecialBindDetailSavePO.getSpecialId());
            lambdaQueryWrapper.eq(AdminSpecialBindDetail::getBindId, adminSpecialBindDetailSavePO.getBindId());
            lambdaQueryWrapper.eq(AdminSpecialBindDetail::getTeacherId, adminSpecialBindDetailSavePO.getTeacherId());
            long count = this.count(lambdaQueryWrapper);
            if (count > 0) {
                throw new KsdBusinessException("该老师已经绑定了!");
            }
            return this.saveOrUpdate(adminSpecialBindDetail);
        }

        return false;
    }

    @Override
    public boolean removeAdminSpecialBindDetailById(Long id) {
        return this.removeById(id);
    }

    /**
     * 根据BindId删除所有明细
     *
     * @param bindId
     * @return
     */
    @Override
    public boolean removeAdminSpecialBindDetailByBindId(Long bindId) {
        LambdaQueryWrapper<AdminSpecialBindDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AdminSpecialBindDetail::getBindId, bindId);
        return this.remove(lambdaQueryWrapper);
    }

    @Override
    public AdminSpecialBindDetailVo getAdminSpecialBindDetailById(Long id) {
        return tranferBo(this.getById(id), AdminSpecialBindDetailVo.class);
    }

    /**
     * 复制
     *
     * @param opid
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean copyData(Long opid) {
        AdminSpecialBindDetail data = this.getById(opid);
        if (Objects.nonNull(data)) {
            data.setId(null);
            data.setStatus(0);
            data.setCreateTime(LocalDateTime.now());
            data.setUpdateTime(LocalDateTime.now());
            this.save(data);
        }
        return true;
    }

}
